import { writable } from "svelte/store";
// 主题状态
const savedTheme = localStorage.getItem("theme");
const prefersDark =
  window.matchMedia &&
  window.matchMedia("(prefers-color-scheme: dark)").matches;
export const theme = writable(savedTheme || (prefersDark ? "dark" : "light"));

theme.subscribe((value) => {
  localStorage.setItem("theme", value);
  document.documentElement.classList.toggle("dark", value === "dark");
});

export function toggleTheme() {
  theme.update((current) => (current === "light" ? "dark" : "light"));
}
